//
//  MooNumber.cpp
//  BattleEngine
//
//  Created by machine on 12-11-1.
//  Copyright (c) 2012年 machine. All rights reserved.
//

#include "MooNumber.h"
#include "stdio.h"

unsigned short MooNumber::mooRandomSeed = 0;

MooNumber::MooNumber() {
    _number = (size_type)0;
}

MooNumber::MooNumber(int num) {
    _number = (size_type)num;
}

MooNumber::MooNumber(long num) {
    _number = (size_type)num;
}

MooNumber::MooNumber(double num) {
    _number = (size_type)num;
}

MooNumber::MooNumber(string num) {
    _number = (size_type)atof(num.c_str());
}

MooNumber::MooNumber(const char * num) {
    _number = (size_type)atof(num);
}

int MooNumber::toInt() const {
    return (int)_number;
}

long MooNumber::toLong() const {
    return (long)_number;
}

double MooNumber::toDouble() const {
    return (double)_number;
}

string MooNumber::toString() const {
    char *ptr = new char();
    string str;
    
    if (int(_number) == _number) {
        sprintf(ptr, "%d", (int)_number);
        str = ptr;
    } else {
        sprintf(ptr, "%f", (size_type)_number);
        str = ptr;
        
        int index = 0;
        for (int i = (int)str.length(); i > 0; i--) {
            if (int(str[i - 1]) != 48) {
                break;
            }
            index++;
        }
        
        str = str.substr(0, str.length() - index);
    }
    
    delete ptr;
    
    return str;
}

MooNumber MooNumber::abs() {
    double number;
    if (_number < 0) {
        number = -_number;
    } else {
        number = _number;
    }
    
    return number;
}

MooNumber MooNumber::ceil() {
    int tmp = (int)_number;
    if (_number > tmp) {
        return (int)(tmp +1);
    } else {
        return (int)(tmp);
    }
}

MooNumber MooNumber::floor() {
    return (int)_number;
}

MooNumber MooNumber::pow(const int &num) {
    if (num > 0) {
        double number = _number;
        for (int i = 0; i < num - 1; i++) {
            number = _number * number;
        }
    } else {
        double number = _number;
        for (int i = 0; i < -num + 1; i++) {
            number = _number / number;
        }
    }
    
    return _number;
}

MooNumber MooNumber::round(int num) {
    // 假设原数据为：1.23432
    // 需要保留2两位小数
    
    // 计算需要保留的位数(pow=100)
    double pow = MooNumber(10).pow(num).toDouble();
    
    // 将数字放大num+1倍(tmp=1234)
    int tmp = (int)(_number * pow * 10);
    
    // 将数字设置为最后一位等于5(tmp5=1235)
    int tmp5 = (int)(((int)(_number * pow) + 0.5) * 10);
    
    // 如果原始数字比tmp5大，则入，否则舍。
    if (tmp >= tmp5) {
        tmp = (int)(tmp / 10) + 1;
    } else {
        tmp = (int)(tmp / 10);
    }
    
    return (size_type)tmp / (size_type)pow;
}


MooNumber MooNumber::rand(MooNumber from, MooNumber to) {
    if (to < from) {
        MooNumber tmp = from;
        from = to;
        to = tmp;
    }
    
    MooNumber::mooRandomSeed += clock();
    srand(MooNumber::mooRandomSeed);
    return from + ::rand() % (to - from + 1);
}

MooNumber MooNumber::operator+(const MooNumber & num) {
    return MooNumber(_number + num.toDouble());
}

MooNumber MooNumber::operator+(const int & num) {
    return MooNumber(_number + num);
}

MooNumber MooNumber::operator+(const long & num) {
    return MooNumber(_number + num);
}

MooNumber MooNumber::operator+(const double & num) {
    return MooNumber(_number + num);
}

MooNumber MooNumber::operator-(const MooNumber & num) {
    return MooNumber(_number - num.toDouble());
}

MooNumber MooNumber::operator-(const int & num) {
    return MooNumber(_number - num);
}

MooNumber MooNumber::operator-(const long & num) {
    return MooNumber(_number - num);
}

MooNumber MooNumber::operator-(const double & num) {
    return MooNumber(_number - num);
}

MooNumber MooNumber::operator*(const MooNumber & num) {
    return MooNumber(_number * num.toDouble());
}

MooNumber MooNumber::operator*(const int & num) {
    return MooNumber(_number * num);
}

MooNumber MooNumber::operator*(const long & num) {
    return MooNumber(_number * num);
}

MooNumber MooNumber::operator*(const double & num) {
    return MooNumber(_number * num);
}

MooNumber MooNumber::operator/(const MooNumber & num) {
    return MooNumber(_number / num.toDouble());
}

MooNumber MooNumber::operator/(const int & num) {
    return MooNumber(_number / num);
}

MooNumber MooNumber::operator/(const long & num) {
    return MooNumber(_number / num);
}

MooNumber MooNumber::operator%(const MooNumber & num) {
    return MooNumber((long)_number % num.toLong());
}

MooNumber MooNumber::operator%(const int & num) {
    return MooNumber((long)_number % num);
}

MooNumber MooNumber::operator%(const long & num) {
    return MooNumber((long)_number % num);
}

MooNumber MooNumber::operator/(const double & num) {
    return MooNumber(_number / num);
}

MooNumber MooNumber::operator+=(const MooNumber & num) {
    return MooNumber(_number += num.toDouble());
}

MooNumber MooNumber::operator+=(const int & num) {
    return MooNumber(_number += num);
}

MooNumber MooNumber::operator+=(const long & num) {
    return MooNumber(_number += num);
}

MooNumber MooNumber::operator+=(const double & num) {
    return MooNumber(_number += num);
}

MooNumber MooNumber::operator-=(const MooNumber & num) {
    return MooNumber(_number -= num.toDouble());
}

MooNumber MooNumber::operator-=(const int & num) {
    return MooNumber(_number -= num);
}

MooNumber MooNumber::operator-=(const long & num) {
    return MooNumber(_number -= num);
}

MooNumber MooNumber::operator-=(const double & num) {
    return MooNumber(_number -= num);
}

MooNumber MooNumber::operator*=(const MooNumber & num) {
    return MooNumber(_number *= num.toDouble());
}

MooNumber MooNumber::operator*=(const int & num) {
    return MooNumber(_number *= num);
}

MooNumber MooNumber::operator*=(const long & num) {
    return MooNumber(_number *= num);
}

MooNumber MooNumber::operator*=(const double & num) {
    return MooNumber(_number *= num);
}

MooNumber MooNumber::operator/=(const MooNumber & num) {
    return MooNumber(_number /= num.toDouble());
}

MooNumber MooNumber::operator/=(const int & num) {
    return MooNumber(_number /= num);
}

MooNumber MooNumber::operator/=(const long & num) {
    return MooNumber(_number /= num);
}

MooNumber MooNumber::operator/=(const double & num) {
    return MooNumber(_number /= num);
}

bool MooNumber::operator>(const MooNumber & num) {
    return _number > num.toDouble();
}

bool MooNumber::operator>(const int & num) {
    return _number > num;
}

bool MooNumber::operator>(const long & num) {
    return _number > num;
}

bool MooNumber::operator>(const double & num) {
    return _number > num;
}

bool MooNumber::operator<(const MooNumber & num) {
    return _number < num.toDouble();
}

bool MooNumber::operator<(const int & num) {
    return _number < num;
}

bool MooNumber::operator<(const long & num) {
    return _number < num;
}

bool MooNumber::operator<(const double & num) {
    return _number < num;
}

bool MooNumber::operator>=(const MooNumber & num) {
    return _number >= num.toDouble();
}

bool MooNumber::operator>=(const int & num) {
    return _number >= num;
}

bool MooNumber::operator>=(const long & num) {
    return _number >= num;
}

bool MooNumber::operator>=(const double & num) {
    return _number >= num;
}

bool MooNumber::operator<=(const MooNumber & num) {
    return _number <= num.toDouble();
}

bool MooNumber::operator<=(const int & num) {
    return _number <= num;
}

bool MooNumber::operator<=(const long & num) {
    return _number <= num;
}

bool MooNumber::operator<=(const double & num) {
    return _number <= num;
}

bool MooNumber::operator==(const MooNumber & num) {
    return _number == num.toDouble();
}

bool MooNumber::operator==(const int & num) {
    return _number == num;
}

bool MooNumber::operator==(const long & num) {
    return _number == num;
}

bool MooNumber::operator==(const double & num) {
    return _number == num;
}


// ------------------------------------------------------

MooNumber operator+(int num1, MooNumber num2) {
    return num1 + num2.toDouble();
}

MooNumber operator+(long num1, MooNumber num2) {
    return num1 + num2.toDouble();
}

MooNumber operator+(double num1, MooNumber num2) {
    return num1 + num2.toDouble();
}

MooNumber operator-(int num1, MooNumber num2) {
    return num1 - num2.toDouble();
}

MooNumber operator-(long num1, MooNumber num2) {
    return num1 - num2.toDouble();
}

MooNumber operator-(double num1, MooNumber num2) {
    return num1 - num2.toDouble();
}

MooNumber operator*(int num1, MooNumber num2) {
    return num1 * num2.toDouble();
}

MooNumber operator*(long num1, MooNumber num2) {
    return num1 * num2.toDouble();
}

MooNumber operator*(double num1, MooNumber num2) {
    return num1 * num2.toDouble();
}

MooNumber operator/(int num1, MooNumber num2) {
    return num1 / num2.toDouble();
}

MooNumber operator/(long num1, MooNumber num2) {
    return num1 / num2.toDouble();
}

MooNumber operator/(double num1, MooNumber num2) {
    return num1 / num2.toDouble();
}

MooNumber operator%(int num1, MooNumber num2) {
    return num1 % num2.toInt();
}

MooNumber operator%(long num1, MooNumber num2) {
    return num1 % num2.toInt();
}

bool operator>(int num1, MooNumber num2) {
    return num1 > num2.toDouble();
}

bool operator>(long num1, MooNumber num2) {
    return num1 > num2.toDouble();
}

bool operator>(double num1, MooNumber num2) {
    return num1 > num2.toDouble();
}

bool operator<(int num1, MooNumber num2) {
    return num1 < num2.toDouble();
}

bool operator<(long num1, MooNumber num2) {
    return num1 < num2.toDouble();
}

bool operator<(double num1, MooNumber num2) {
    return num1 < num2.toDouble();
}

bool operator>=(int num1, MooNumber num2) {
    return num1 >= num2.toDouble();
}

bool operator>=(long num1, MooNumber num2) {
    return num1 >= num2.toDouble();
}

bool operator>=(double num1, MooNumber num2) {
    return num1 >= num2.toDouble();
}

bool operator<=(int num1, MooNumber num2) {
    return num1 <= num2.toDouble();
}

bool operator<=(long num1, MooNumber num2) {
    return num1 <= num2.toDouble();
}

bool operator<=(double num1, MooNumber num2) {
    return num1 <= num2.toDouble();
}

bool operator==(int num1, MooNumber num2) {
    return num1 == num2.toDouble();
}

bool operator==(long num1, MooNumber num2) {
    return num1 == num2.toDouble();
}

bool operator==(double num1, MooNumber num2) {
    return num1 == num2.toDouble();
}


